package com.base.cn.platform.os.service.order.callback;

import com.base.cn.platform.os.common.utils.ObjectUtils;
import com.base.cn.platform.os.common.utils.order.OrderStatus;
import com.base.cn.platform.os.entity.order.order.Order;
import com.base.cn.platform.os.entity.order.order.OrderDetails;
import com.base.cn.platform.os.entity.user.account.CusUserAccount;
import com.base.cn.platform.os.service.user.account.CusUserAccountBiz;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 退费后续处理
 *
 * @author s.li
 * @create 2018-05-10-19:15
 */
@Service
public class RefundCallbackHandle {
    @Autowired
    private OrderDetailsStatusHandle orderDetailsStatusHandle;
    @Autowired
    private CusUserAccountBiz cusUserAccountBiz;
    /**
     * 订单退费后续处理
     * @param order 订单对象
     * @param sysUserId 管理员ID
     */
    public void refundHandle(Order order, BigDecimal sysUserId){
        List<OrderDetails> detailsList = order.getOrderDetailsList();
        Map<Boolean,List<OrderDetails>> orderDetailsListMap = detailsList.stream().collect(Collectors.groupingBy(e->e.getDetailStatus().equals(OrderStatus.REFUND)));
        //获取未退费的流水
        List<OrderDetails> orderDetailsList = orderDetailsListMap.get(false);
        if(ObjectUtils.isNotEmpty(orderDetailsList)){
            double refundPrice = 0D;
            for(OrderDetails details : orderDetailsList){
                refundPrice += details.getRealPrice();
            }
            double cashAmount = order.getCashAmount();
            double vmAmount = order.getVmAmount();
            double orderRealPrice = order.getRealPrice()-order.getFreightPrice();
            double _cashAmount = orderDetailsStatusHandle.countRefundPrice(refundPrice,cashAmount,orderRealPrice);
            double _vmAmount = orderDetailsStatusHandle.countRefundPrice(refundPrice,vmAmount,orderRealPrice);
            CusUserAccount userAccount = null;
            if(_cashAmount>0 || _vmAmount>0){
                userAccount = cusUserAccountBiz.queryUserAccountByUserId(order.getUserId(),false);
            }
            if(userAccount != null){
                BigDecimal userBalance = (userAccount.getBalance() == null ? BigDecimal.ZERO : userAccount.getBalance()).setScale(2,BigDecimal.ROUND_HALF_DOWN);
                BigDecimal userCashAmount = (userAccount.getCashAmount() == null ? BigDecimal.ZERO : userAccount.getCashAmount()).setScale(2,BigDecimal.ROUND_HALF_DOWN);
                BigDecimal userVmAmount = (userAccount.getVmAmount() == null ? BigDecimal.ZERO : userAccount.getVmAmount()).setScale(2,BigDecimal.ROUND_HALF_DOWN);
                if(_cashAmount>0){
                    userBalance = userBalance.add(new BigDecimal(_cashAmount));
                    userCashAmount = userCashAmount.add(new BigDecimal(_cashAmount));
                    userAccount.setBalance(userBalance);
                    userAccount.setCashAmount(userCashAmount);
                    cusUserAccountBiz.tx_updateUserAccountAmount(userAccount,sysUserId,9,_cashAmount,order);
                }
                if(_vmAmount>0){
                    userBalance = userBalance.add(new BigDecimal(_vmAmount));
                    userVmAmount = userVmAmount.add(new BigDecimal(_vmAmount));
                    userAccount.setBalance(userBalance);
                    userAccount.setVmAmount(userVmAmount);
                    cusUserAccountBiz.tx_updateUserAccountAmount(userAccount,sysUserId,10,_vmAmount,order);
                }
            }
        }
    }
}
